import os, stat
import urllib.request
import requests
import threading

headers = {
    'Cookie': 'UM_distinctid=17c15b16b7275-02b55430879ff9-2343360-1fa400-17c15b16b7376d; LANGUAGE=zh_CN; PHPSESSID=7lq26boej509u9jql49iege041; _ga=GA1.2.368327117.1632464693; _gid=GA1.2.1571488697.1632962690; currentPage=262%7B%7C%7D%2Fproduct%2Fdevelop%2Flist%3Fquick%3D262%7B%7C%7D%E4%BA%A7%E5%93%81%E8%B5%84%E6%96%99%E7%AE%A1%E7%90%86; SERVERID=d2a587b1213719176db2c1624a474613|1632963735|1632621740',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',

}

skuall=[]
def go():
    listall = []


    # url = 'http://sw.eccang.com/product/develop/list/page/1/pageSize/10'
    # res_to = requests.post(url, headers=headers, verify=False).json()
    # print(res_to)


    total1 = 1#int(total)
    for num in range(1, total1 + 1):
        # res(num)
        ts = threading.Thread(target=res, args=(num,))
        listall.append(ts)
    for i_ts1 in listall:
        i_ts1.start()  # 启动线程

    for i_ts in listall:
        i_ts.join()  # 关闭线程


def res(num):
    url = 'http://sw.eccang.com/product/develop/list/page/{}/pageSize/10'.format(num)
    res1 = requests.post(url, headers=headers, verify=False).json()
    for i in res1["data"]:
        print(i["product_barcode"], i["product_img"])

        if i["product_img"]!='/images/base/noimg.jpg' and i["product_img"][-3:]!='png':
            img_url = i["product_img"]
            file_path = 'D:/book/img'
            file_name = i["product_barcode"]

            # 是否有这个路径
            if not os.path.exists(file_path):
                # 创建路径
                os.makedirs(file_path)
                # 获得图片后缀
            file_suffix = os.path.splitext(img_url)[1]
            # print(file_suffix)
            # 拼接图片名（包含路径）
            filename = '{}{}{}{}'.format(file_path, os.sep, file_name, file_suffix)
            # print(filename)
            # 下载图片，并保存到文件夹中
            urllib.request.urlretrieve(img_url, filename=filename)
            skuall.append(i["product_barcode"])





import xlsxwriter

def all():
    workbook = xlsxwriter.Workbook('images.xlsx')
    worksheet = workbook.add_worksheet()
    for i in range(1,len(skuall)+1):

        # 加宽第一列使文本更清晰。
        worksheet.set_column('A:A', 10)
        # 加宽图片列

        worksheet.set_column(1, 45)
        worksheet.set_default_row(45)

        worksheet.insert_image('A{}'.format(i), r'D:\book\img\{}.jpg'.format(skuall[i-1]),{'x_scale': 0.1, 'y_scale': 0.07})
        worksheet.write('B{}'.format(i),'{}'.format(skuall[i-1]))
    workbook.close()


go()
all()